package com.cash.loan.common.result;

import com.alibaba.fastjson.annotation.JSONField;

import java.io.Serializable;

/**
 * 返回结果封装
 * @author Administrator
 * @date 2017/08/20
 */
public class ServerResponse<T> implements Serializable {

    private int code;

    private String message;

    private T data;

    private ServerResponse(int code){
        this.code = code;
    }

    private ServerResponse(int code, T data){
        this.code = code;
        this.data = data;
    }

    private ServerResponse(int code, String message, T data){
        this.code = code;
        this.message = message;
        this.data = data;
    }

    private ServerResponse(int code, String message){
        this.code = code;
        this.message = message;
    }

    @JSONField(serialize = false)
    public boolean isSuccess(){
        return this.code == ResponseCode.SUCCESS.getCode();
    }

    public int getCode(){
        return code;
    }
    public T getData(){
        return data;
    }
    public String getMessage(){
        return message;
    }


    public static <T> ServerResponse<T> getSuccess(){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> ServerResponse<T> getSuccess(String message){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),message);
    }

    public static <T> ServerResponse<T> getSuccess(T data){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),data);
    }

    public static <T> ServerResponse<T> getSuccess(String message, T data){
        return new ServerResponse<T>(ResponseCode.SUCCESS.getCode(),message,data);
    }

    public static <T> ServerResponse<T> getSuccess(int successCode,String message){
        return new ServerResponse<T>(successCode,message);
    }

    public static <T> ServerResponse<T> getErrorMessage(){
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getMessage());
    }

    public static <T> ServerResponse<T> getErrorMessage(String errorMessage){
        return new ServerResponse<T>(ResponseCode.ERROR.getCode(),errorMessage);
    }

    public static <T> ServerResponse<T> getErrorCodeMessage(int errorCode,String errorMessage){
        return new ServerResponse<T>(errorCode,errorMessage);
    }

}
